﻿using System;
using System.Linq;
using AutoMapper;
using Common.Enumerations;
using Common.Views;
using Dto.ApiResponses;
using Dto.ApiResponses.AttachmentResponses;
using Dto.ApiResponses.ContactResponses;
using Dto.ApiResponses.MemberResponses;
using Dto.ApiResponses.NoteResponses;
using PetaPoco;
using Sanelib.Common.Helpers;
using Sanelib.DataView;
using Sanelib.Dto.Enumerations;

namespace WebApp.ModelService.Impl
{
    public class MemberModelService : IMemberModelService
    {
        private readonly IViewRepository<MemberView> _memberViewRepository;
        private readonly IViewRepository<AssignedContactView> _assignedContactViewRepository;
        private readonly IViewRepository<NoteView> _noteViewRepository;
        private readonly IViewRepository<AttachmentView> _attachmentViewRepository;

        public MemberModelService(IViewRepository<MemberView> memberRepository, IViewRepository<NoteView> noteViewRepository, IViewRepository<AttachmentView> attachmentRepository, IViewRepository<AssignedContactView> assignedContactViewRepository)
        {
            _memberViewRepository = memberRepository;
            _noteViewRepository = noteViewRepository;
            _assignedContactViewRepository = assignedContactViewRepository;
            _attachmentViewRepository = attachmentRepository;
        }

        public PageResponse<MemberLine> GetPageBySpecification(SearchSpecification specification = null)
        {
            if (specification == null)
            {
                specification = new SearchSpecification
                {
                    ColumnName = "Code",
                    ColumnValue = "%",
                    FilterType = "like"
                };
            }

            var memberViewPage = _memberViewRepository.SearchBySpecification(specification, null);
            var response = Mapper.Map<Page<MemberView>, PageResponse<MemberLine>>(memberViewPage);
            return response;
        }

        public MemberDetailResponse GetMemberDetailById(Guid? id)
        {
            var response = new MemberDetailResponse
            {
                Roles = Role.GetAllPaired(),
                UserStatuses = UserStatus.GetAllPaired()
            };

            if (!id.HasValue) return response;

            var memberView = _memberViewRepository.GetById(id.Value);
            var contactViews = _assignedContactViewRepository.GetAllFor(Property.Of<AssignedContactView>(x => x.ReferenceId), id);
            var noteViews = _noteViewRepository.GetAllFor(Property.Of<NoteView>(x => x.ReferenceId), id);
            var attachmentViews = _attachmentViewRepository.GetAllFor(Property.Of<AttachmentView>(x => x.ReferenceId), id);
            
            response.Detail = Mapper.Map<MemberView, MemberResponse>(memberView);
            response.ContactLines = contactViews.Select(Mapper.Map<AssignedContactView, AssignedContactLine>).ToArray();
            response.Notes = noteViews.Select(Mapper.Map<NoteView, NoteLine>).ToArray();
            response.Attachments = attachmentViews.Select(Mapper.Map<AttachmentView, AttachmentLine>).ToArray();
            return response;
        }
    }
}